package com.mongo.api;

import com.tanhua.dubbo.mongo.UserLocationAPI;
import com.tanhua.mongo.Places;
import com.tanhua.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class UserLocationAPIImpl implements UserLocationAPI {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void uploadLocation(Long userId, Double longitude, Double latitude, String addrStr) {
        //如果首次注册，还从来没有上报过地理位置，我们就做新增，如果已经上报过了，那就是修改
        //1 查询
        Criteria criteria = Criteria.where("userId").is(userId);
        Query query = new Query(criteria);
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        //2 判断
        if (userLocation==null){
            //3 执行新增
            userLocation = new UserLocation();
            userLocation.setId(ObjectId.get());
            userLocation.setUserId(userId);
            userLocation.setLocation(new GeoJsonPoint(longitude,latitude));
            userLocation.setAddress(addrStr);
            userLocation.setCreated(System.currentTimeMillis());
            userLocation.setUpdated(System.currentTimeMillis());
            userLocation.setLastUpdated(System.currentTimeMillis());

            mongoTemplate.save(userLocation);

        }else {
            //3 执行更新
            Update update = Update.update("location",new GeoJsonPoint(longitude,latitude));
            update.set("updated",System.currentTimeMillis());
            update.set("lastedUpdated",System.currentTimeMillis());
            update.set("address",addrStr);

            mongoTemplate.updateFirst(query,update,UserLocation.class);

        }


    }

    /**
     * 获取当前登录用户的地理位置
     *
     * @param distance
     * @return
     */
    @Override
    public List<Long> nearSearch(Long userId,String distance) {
        //查找当前登录用户的位置
        Criteria criteria = Criteria.where("userId").is(userId);
        Query query = new Query(criteria);
        UserLocation ul = mongoTemplate.findOne(query, UserLocation.class);
        if (ul==null){
            return null;
        }
        //1 给定坐标，相当于圆心
        GeoJsonPoint point = ul.getLocation();
        //2 构造半径
        //Double ddistance = Double.valueOf(distance);

        Distance distance1 = new Distance(Double.valueOf(distance)/1000, Metrics.KILOMETERS);
        //3 画圆
        Circle circle = new Circle(point, distance1);
        //4 构造query对象
        Query query1 = new Query(Criteria.where("location").withinSphere(circle));
        //5 查询
        List<UserLocation> userLocationList = mongoTemplate.find(query1, UserLocation.class);
        userLocationList.forEach(System.out::println);

        return userLocationList.stream().map(u -> u.getUserId()).collect(Collectors.toList());
    }
}
